package com.Akronmcp.hopMessage;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.Akronmcp.hopTextAPI.p2.HCondition;
import com.Akronmcp.hopTextAPI.p2.HManager;
import com.Akronmcp.hopTextAPI.p2.HPar;
import com.Akronmcp.hopTextAPI.p2.HPar2;
import com.Akronmcp.hopTextAPI.p2.HopBar;
import com.Akronmcp.hopTextAPI.p2.HopBook;
import com.Akronmcp.hopTextAPI.p2.HopChat;
import com.Akronmcp.hopTextAPI.p2.HopTitle;


public class HmLoader {

	static FileConfiguration conf = HmM.conf;
	static FileConfiguration lang = HmM.lang;
	static HManager hta = HmM.hta;
	
	public static File df = HmM.df;
	
	
	public HmLoader() throws Exception
	{
		load_conf();
	}
	
	/**根据文件名载入公告线程对象*/
	public void load_hm(String hmf)
	{
		YamlConfiguration hmyf = YamlConfiguration.loadConfiguration(new File(df,hmf));
		
		
		if(hmyf==null)
		{
			System.out.print("未找到文件"+hmf);
			return;
		}
		
		ConfigurationSection part0 = hmyf.getConfigurationSection("Messages");
		//获取配置下的子节点列表
		
		if(part0!=null)
	    for(String thtid:part0.getKeys(false))
	    {
	    	String path = "Messages." + thtid + ".";
	    	//节点路径
	    	
	    	HopMess hm = new HopMess();
	    	//公告线程实例
	    	hm.setId(thtid);
	    	
	    	//HPar de = new HPar(hta);
	    	
	    	String pre = "";
	    	String suf = "";
	    	//前后缀
	    	
	    	String tx =  hmyf.getString( path +"text");
	    	String pre2 = hmyf.getString( path +"prefix");
	    	String suf2 = hmyf.getString( path +"suffix");

	    	String inte = hmyf.getString( path +"interval");
	    	
	    	String type = hmyf.getString( path +"type");
	    	
	    	String module = hmyf.getString( path +"module");
	    	
	    	boolean en = hmyf.getBoolean(path+"enable");
	    	boolean papi = hmyf.getBoolean(path+"papi");
	    		    	
	    	boolean random = hmyf.getBoolean(path+"random");
	    	
	    	String need = hmyf.getString(path + "need");
    		String notneed = hmyf.getString(path + "notneed");

	    	String begin = hmyf.getString(path + "begin");
	    	String end = hmyf.getString(path + "end");
 
	    	String daybegin = hmyf.getString(path + "daybegin");
	    	String dayend = hmyf.getString(path + "dayend");

	    	boolean web = hmyf.getBoolean(path+"web");
	    	String url = hmyf.getString(path + "url");
	    	String listname = hmyf.getString(path + "listname");
	    	
	    	Date d1 = null;
	    	Date d2 = null;
	    	
	    	if(module!=null)
			{//保存模板信息
				hm.setModule(module);
			}
	    	
	
	    	
    		if(begin!=null)
    		{
    			SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 
    			
    			 try {
				  d1 = ft.parse(begin);
				} catch (ParseException e) {
					e.printStackTrace();
				}
    		}
	    	
    		if(end!=null)
    		{
    			SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd"); 
    			
    			 try {
				  d2 = ft.parse(end);
				} catch (ParseException e) {
					e.printStackTrace();
				}
    		}
	    	
    		hm.setDate(d1, d2);
    		
    		
    		 Calendar c1 = null;
    		 Calendar c2 = null;
    		 
    		if(daybegin!=null)
    		{
    			SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd-HH:mm"); 
    			
    			 try {
    				 Calendar t =  Calendar.getInstance();
				     t.setTime(ft.parse("2000-10-10-"+daybegin));
				     c1=t;
				} catch (ParseException e) {
					e.printStackTrace();
				}
    		}
    		
    		if(dayend!=null)
    		{
    			SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd-HH:mm"); 
    			
    			 try {
    				 Calendar t =  Calendar.getInstance();
				     t.setTime(ft.parse("2000-10-10-"+dayend));
				     
				     c2=t;
				} catch (ParseException e) {
					e.printStackTrace();
				}
    		}
    		
    		hm.setDaily(c1, c2);
    		//读入
    		
    		if(need!=null)
    		{//读取条件(条件:参数1,参数2,...)
    			String cons[] = need.trim().split("\\n");
    			List<HCondition> hcl = new ArrayList<HCondition>();
    			
    			for(String con:cons)
    			{
    				
    			  String str[] = con.trim().split(":");
    			  if(str.length==2)
    			  {
    				  String args[] = str[1].split(",");
    				  hcl.add(new HCondition(str[0], args));
    			  }else
    			  {
    				  continue;
    			  }
    			  
    			 
    			}
    			hm.setConditions(hcl);
    		}
    		
    		if(notneed!=null)
    		{//读取反向条件(条件:参数1,参数2,...)
    			String cons[] = notneed.trim().split("\\n");
    			List<HCondition> hcl = new ArrayList<HCondition>();
    			
    			for(String con:cons)
    			{
    				
    			  String str[] = con.trim().split(":");
    			  if(str.length==2)
    			  {
    				  String args[] = str[1].split(",");
    				  hcl.add(new HCondition(str[0], args));
    			  }else
    			  {
    				  continue;
    			  }
    			  
    			 
    			}
    			hm.setObConditions(hcl);
    		}
	    	
	    	if(inte!=null)
	    	hm.setInterval(Integer.valueOf(inte)*20);
	    		
	    	if(en)
	    	hm.setEnable(en);
	    	
	    	if(random)
	    	hm.setRandom(random);
	    	
	    	if(pre2!=null)
	    		pre=pre2;
	    	if(suf2!=null)
	    		suf=suf2;
	    	//载入基本信息
	    	

	    	
	    	if(tx!=null)
	    	{//载入公告列表
	    		
	    		pre = pre.replace("&", "§");
	    		suf = suf.replace("&", "§");
	    		
	    		
	    		String ta[] = tx.trim().replace("&", "§").split("\\n//\\n");
	    		//通过//分割节点,生成公告列表
	    			
	    			if(type==null||type.equals("chat"))
		    		{
	    				
	    				for(String text:ta)
	    				{//遍历公告列表
	    					
	    				  HPar hc = checkToHT(text);
	    				  //识别标签
	    				  
	    				  if(hc==null)
	    				  {
	    					  hc = new HopChat(hta);
	    					  
	    					  hc.setPapi(papi);
	    					  
	    					  hc.setText(pre+text+suf);
	    					  ((HopChat) hc).create();
	    					  //创建对象
	    					  
	    				  }
	    				  hm.addObject(hc);
	    				
	    				
	    				}
		    			
	    				
		    		}else if (type.equals("bar"))
		    		{
		    			for(String text:ta)
	    				{//遍历公告列表
		    				
		    				HPar hb = checkToHT(text);
		    				//识别标签
		    				
		    				if(hb==null)
		    				{
			    				hb = new HopBar(hta);
			    				
			    				hb.setPapi(papi);
			    				 
			    				hb.setText(pre+text+suf);
			    				//创建对象
			    				
			    				if(module!=null)
			    				{//设置模板
			    					((HPar2) hb).setModule(module);
			    				}
		    				}
		    			   hm.addObject(hb);
		    			   //加入列表
	    				}
		    		
		    					
		    		}else if (type.equals("book"))
		    		{
		    			for(String text:ta)
	    				{
		    				HPar hb = checkToHT(text);
		    				
		    				if(hb==null)
			    			{
		    					hb = new HopBook(hta);
		    					
		    					hb.setPapi(papi);
		    					
		    					hb.setText(pre+text+suf);
			    			}
		    				hm.addObject(hb);
	    				}
		    			
		    			
		    		}else
		    		{
		    			for(String text:ta)
	    				{
		    				HPar ht = checkToHT(text);
		    				
		    				if(ht==null)
		    				{
		    					
		    				    ht = new HopTitle(hta);
		    					
		    					ht.setPapi(papi);
		    					
		    					ht.setText(pre+text+suf);
		    					((HopTitle) ht).setType(type);
		    					if(module!=null)
		    					{
		    						((HPar2) ht).setModule(module);
		    					}
		    				}
		    				
	    				    hm.addObject(ht);
	    				}
		    			
		    		}
	    			
	    			
	    	}
	    	
	    	if(web&&url!=null&&listname!=null)
	    	{//若网络模式开启,参数齐全
	    		if(!hm.getHl().isEmpty())
	    		hm.setWeb(url,listname,hm.getHl().get(0).getText(),hm.getHl().get(0));
	    		//设置网络参数(地址,以现集合内首个公告作为模板)
	    		
	    		ConfigurationSection args = hmyf.getConfigurationSection(path+"args");
	    		
	    		if(args!=null)
	    		{//输入url参数
	    			Map<String, String> theargs = new HashMap<String, String>();
	    			for(String arg:args.getKeys(false))
	    			{
	    				theargs.put(arg,hmyf.getString(path+"args."+arg));
	    			}
	    			
	    			hm.setWebArgs(theargs);
	    		}
	    		
	    	}
	    	
	    	
	    	
	    	
	    	hm.addToMap();
	    	//加入列表并启用
	    }
	
	}
	
	/**载入插件配置*/
	public void load_conf() throws Exception
	{
	
			
			 conf = null;
	         lang = null;
	         //清空
	         
	         conf = YamlConfiguration.loadConfiguration(new File(df,"config.yml"));
	        
	         String langtype = conf.getString("langtype");
	    	 lang = YamlConfiguration.loadConfiguration(new File(df+"//lang",langtype+".yml"));
	        //载入语言
	         HmM.lang = lang;
			//确定指针
			
	         String in = conf.getString("input");
	         //载入文件列表
	         
	         if(in!=null)
	         {
	        	 for(HopMess hm:HopMess.hmMap.values())
	        	 {//关闭所有公告线程
	        		 hm.setEnable(false);
	        	 }
	        	 HopMess.hmMap.clear();	        	 
	        	 hta.clearAll();
	        	 //清空
	        	 
	        	 String ins[] = in.split("\\n");
	        	 //生成文件名列表
	        	 
	        	 HmTimer.setOff();
	        	 //防止遍历冲突
	        	 
	        	 for(String file:ins)
	        	 {//遍历载入hta对象
	        		 hta.load(new File(df,file.trim()));
	        	 }
	        	 for(String file:ins)
	        	 {//遍历载入hm,公告线程对象
	        		  load_hm(file.trim());
	        	 }
	        	 
	        	 HmTimer.goStart();
	         }
	        	 
			 
	         String onjoin = conf.getString("Listeners.OnJoin");
	         String onWorld = conf.getString("Listeners.OnWorld");
	         String onDeath = conf.getString("Listeners.OnDeath");
	         //读取监听所发送的公告
	         
	         if(onjoin!=null)
	         {
	        	String[] joinl = onjoin.trim().split("\\n");
	        	for(String text:joinl)
	        	{
	        		HopMess  hm = HopMess.hmMap.get(text);
	        		
	        		if(hm!=null)
	        		{
	        			HmListener.LJoinhml.add(hm);
	        			//加入公告到监听的发送列表
	        		}else
	        		{
	        			System.out.println("未创建公告列:text!!");
	        		}
		
	        	}
	         }
	         
	         if(onWorld!=null)
	         {
	        	String[] worldl = onWorld.trim().split("\\n");
	        	for(String text:worldl)
	        	{
	        		HopMess  hm = HopMess.hmMap.get(text);
	        		
	        		if(hm!=null)
	        		{
	        			HmListener.LWorldhml.add(hm);
	        		}else
	        		{
	        			System.out.println("未创建公告列:text!!");
	        		}
		
	        	}
	         }
	         
	         if(onDeath!=null)
	         {
	        	String[] diel = onDeath.trim().split("\\n");
	        	for(String text:diel)
	        	{
	        		HopMess  hm = HopMess.hmMap.get(text);
	        		
	        		if(hm!=null)
	        		{
	        			HmListener.LDeathhml.add(hm);
	        		}else
	        		{
	        			System.out.println("未创建公告列:text!!");
	        		}
		
	        	}
	         }
	         
	         
			
	
	}
	
	/**解析文本,返回一个hoptext对象,如输入hchat:exp
	 * 返回一个主引擎列表中id为exp的hopchat对象,若不存在则返回null*/
	public HPar checkToHT(String text)
	{
		
		String types[] = {"hchat:","htitle:","hbar:","hbook:"};
		
		HPar hp = null;
		
	f1 :for(String type:types)
		{
			if(text.startsWith(type))
			{
				text = text.replace(type,"");
				switch(type)
				{
				case "hchat:":
				hp = hta.getHopChat(text);	
				break f1;
				case "htitle:":
				hp = hta.getHopTitle(text);
				break f1;
				case "hbar:":
				hp = hta.getHopBar(text);
				break f1;
				case "hbook:":
				hp = hta.getHopBook(text);
				break f1;

				}
			}
		}
		

		
		return hp;
	}
	
	
}
